//
//  MovieDetailViewController.m
//  Kokozu
//
//  Created by Bismarrck on 11-7-12.
//  Copyright 2011年 Tsinghua University. All rights reserved.
//

#import "MovieDetailViewController.h"
#import "CustomTabBarViewController.h"
#import "CustomTabBar.h"

@implementation MovieDetailViewController

@synthesize dataPicker = dataPicker_;
@synthesize datePicker = datePicker_;
@synthesize scrollView = scrollView_;
@synthesize movieRecord = movieRecord_;
@synthesize optionTable = optionTable;
@synthesize actionSheet = actionSheet_;
@synthesize goButton = goButton_;
@synthesize priceLabel = priceLabel_;
@synthesize customCell;
@synthesize cellNib;
@synthesize selectedDate;
@synthesize wantTicketsNum;

#pragma mark - View lifecycle

- (id)initWithMovieRecord:(MovieRecord *)aRecord
{
    self = [super init];
    if (self) {
        self.movieRecord = aRecord;
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
    }
    
    return self;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    
    dataPicker_ = nil;
    datePicker_ = nil;
    scrollView_ = nil;
    movieRecord_ = nil;
    goButton_ = nil;
    actionSheet_ = nil;
    customCell = nil;
    cellNib = nil;
}

- (void)dealloc
{
    [datePicker_ release];
    [dataPicker_ release];
    [scrollView_ release];
    [movieRecord_ release];
    [goButton_ release];
    //[optionTable release];
    [actionSheet_ release];
    [customCell release];
    [cellNib release];
    
    [super dealloc];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)viewDidLoad
{
    // Set the navigation bar title
    [self addTitleToNavigationBar:@"优惠信息"];
    
    // call its super to load the view
    [super viewDidLoad];
}

#pragma mark -
#pragma mark - Interface Builder Method

- (IBAction)goButtonPressed
{
    
}

#pragma mark -
#pragma mark - Table View Delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}

#pragma mark -
#pragma mark - Methods

- (UIToolbar *)actionSheetToolBar
{
    UIToolbar *actionToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
    [actionToolBar setBarStyle:UIBarStyleDefault];
    
    NSMutableArray *barItems = [[NSMutableArray alloc] initWithCapacity:3];
    
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"选择" 
                                                                   style:UIBarButtonItemStyleDone 
                                                                  target:self
                                                                  action:@selector(selectionDone)];
    [barItems addObject:doneButton];
    [doneButton release];
    
    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexibleSpace];
    [flexibleSpace release];
    
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"取消" 
                                                                     style:UIBarButtonItemStyleBordered 
                                                                    target:self 
                                                                    action:@selector(selectionCancel)];
    [barItems addObject:cancelButton];
    [cancelButton release];
    
    actionToolBar.items = barItems;
    [barItems release];
    
    return actionToolBar;
}

- (UIActionSheet *)actionSheetWithType:(ActionSheetType)aType
{
    UIActionSheet *actSheet = [[UIActionSheet alloc] initWithTitle:nil 
                                                          delegate:nil 
                                                 cancelButtonTitle:nil 
                                            destructiveButtonTitle:nil 
                                                 otherButtonTitles:nil, nil];
    actSheet.actionSheetStyle = UIActionSheetStyleDefault;
    
    CGRect frame = CGRectMake(0.0f, 40.0f, 0.0f, 0.0f);
    dataPicker_ = [[UIPickerView alloc] initWithFrame:frame];
    self.dataPicker.delegate = self;
    self.dataPicker.dataSource = self;
    self.dataPicker.showsSelectionIndicator = YES;
    [self pickerView:self.dataPicker didSelectRow:0 inComponent:0];
    [actSheet addSubview:self.dataPicker];
    
    UIToolbar *toolBar = [self actionSheetToolBar];
    [actSheet addSubview:toolBar];
    
    [actSheet showInView:self.view];
    [actSheet setBounds:CGRectMake(0.0f, 0.0f, 320.0f, 485.0f)];
    
    return actSheet;
}

- (NSString *)priceLabelwithPrice:(int)price
{
    return [NSString stringWithFormat:@"%d %d", price/10, price%10];
}

- (void)makePhoneCallWithTel:(NSString *)telNumber
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", telNumber]]];
}

#pragma mark -
#pragma mark - Picker View Data Source

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return 10;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [NSString stringWithFormat:@"%d张", row+1];
}

#pragma mark -
#pragma mark - Picker View Delegate

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    self.wantTicketsNum = row;
}

#pragma mark -
#pragma mark - Private Methods

- (void)selectionCancel
{
    [self.actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}

- (void)selectionDone
{
    
}

- (void)eventForDatePicker:(id)sender
{
    UIDatePicker *picker = (UIDatePicker *)sender;
    self.selectedDate = picker.date;
}

- (void)eventForSlider:(id)sender
{
    UISlider *slider = (UISlider *)sender;
    
    self.priceLabel.text = [NSString stringWithFormat:@"%d元", slider.value];
    self.priceLabel.font = [UIFont systemFontOfSize:56.0f];
    self.priceLabel.backgroundColor = [UIColor clearColor];
    self.priceLabel.textAlignment = UITextAlignmentCenter;
}

@end
